package tools;

import sw6.visualschedule.entities.IVisualScheduleElement;
import sw6.visualschedule.extendedViews.CustomEntry;
import sw6.visualschedule.persistence.AbstractDataRepo;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.text.method.SingleLineTransformationMethod;
import android.widget.EditText;

public class DialogHelper {
	
	private DialogHelper() { }

	public static <T extends IVisualScheduleElement> void showTitleEntryDialog(final AbstractDataRepo<T> repo, final T instance, Activity activity, final CustomEntry entry) {
	    final AlertDialog.Builder alert = new AlertDialog.Builder(activity);
        final EditText input = new EditText(activity);
        input.setInputType(android.text.InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE);
        input.setText(instance.getTitle());
        input.setTransformationMethod(SingleLineTransformationMethod.getInstance());
        alert.setView(input);
        
        alert.setPositiveButton(activity.getString(android.R.string.ok), new DialogInterface.OnClickListener() {
			public void onClick(final DialogInterface dialog, final int whichButton) {
                final String value = input.getText().toString().replace('\n',' ').trim();
                instance.setTitle(value);
                repo.update((T)instance);
                entry.getSummary().setText(value);
            }
        });
        
        alert.setNegativeButton(activity.getString(android.R.string.cancel),null);
        
        alert.show();
	}
}